package yadong.example.code;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

public class JSONArrayWorkaround {

	public static void main( String[] args ) throws UnsupportedEncodingException{
	 
		 String encryptedJsonString = "%5B%7B%22id%22%3A%221060226%22%2C%22type%22%3A%22profile%22%7D%2C%7B%22id%22%3A%221061165%22%2C%22type%22%3A%22profile%22%7D%2C%7B%22id%22%3A%221106318%22%2C%22type%22%3A%22profile%22%7D%2C%7B%22id%22%3A%221158086%22%2C%22type%22%3A%22profile%22%7D%2C%7B%22id%22%3A%221241920%22%2C%22type%22%3A%22profile%22%7D%2C%7B%22id%22%3A%221247028%22%2C%22type%22%3A%22profile%22%7D%2C%7B%22id%22%3A%221206087%22%2C%22type%22%3A%22profile%22%7D%5D";
		 String jsonString = null;
		 jsonString = URLDecoder.decode(encryptedJsonString,"UTF-8");
		 JSONArray idList=(JSONArray)JSONSerializer.toJSON(jsonString);
		 
		 System.out.println("jsonString="+jsonString);
		 System.out.println("idList="+idList);
		 JSONObject jsonObj = new JSONObject();
		 jsonObj.put("id", "999999");
		 jsonObj.put("type", "PPPPPP");
		 idList.add(0, jsonObj);
		 Map<String, Object> idMap=null;
		 for (Object object : idList) {
			 idMap = (JSONObject)object;
			 System.out.println("id="+idMap.get("id")+"; type="+idMap.get("type"));
		}
		 jsonString = idList.toString();
		 System.out.println("uncryptedJsonString="+jsonString);
		 System.out.println("encryptedJsonString="+URLEncoder.encode(jsonString, "UTF-8"));
	 
	 }
}
